home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / lib-src / make-docfile.c < prev    next >
C/C++ Source or Header  |  1993-08-12  |  17KB  |  708 lines

  1. /* Generate doc-string file for GNU Emacs from source files.
  2.    Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* The arguments given to this program are all the C and Lisp source files
  21.  of GNU Emacs.  .elc and .el and .c files are allowed.
  22.  A .o file can also be specified; the .c file it was made from is used.
  23.  This helps the makefile pass the correct list of files.
  24.  
  25.  The results, which go to standard output or to a file
  26.  specified with -a or -o (-a to append, -o to start from nothing),
  27.  are entries containing function or variable names and their documentation.
  28.  Each entry starts with a ^_ character.
  29.  Then comes F for a function or V for a variable.
  30.  Then comes the function or variable name, terminated with a newline.
  31.  Then comes the documentation for that function or variable.
  32.  */
  33.  
  34. #include <stdio.h>
  35. #ifdef WINDOWSNT
  36. #include <direct.h>
  37. #endif
  38.  
  39. FILE *outfile;
  40.  
  41. main (argc, argv)
  42.      int argc;
  43.      char **argv;
  44. {
  45.   int i;
  46.   int err_count = 0;
  47.  
  48.   outfile = stdout;
  49.  
  50.   /* If first two args are -o FILE, output to FILE.  */
  51.   i = 1;
  52.   if (argc > i + 1 && !strcmp (argv[i], "-o"))
  53.     {
  54.       outfile = fopen (argv[i + 1], "w");
  55.       i += 2;
  56.     }
  57.   if (argc > i + 1 && !strcmp (argv[i], "-a"))
  58.     {
  59.       outfile = fopen (argv[i + 1], "a");
  60.       i += 2;
  61.     }
  62.   if (argc > i + 1 && !strcmp (argv[i], "-d"))
  63.     {
  64.       chdir (argv[i + 1]);
  65.       i += 2;
  66.     }
  67.  
  68.   for (; i < argc; i++)
  69.     err_count += scan_file (argv[i]);    /* err_count seems to be {mis,un}used */
  70. #ifndef VMS
  71.   exit (err_count);            /* see below - shane */
  72. #endif /* VMS */
  73. }
  74.  
  75. /* Read file FILENAME and output its doc strings to outfile.  */
  76. /* Return 1 if file is not found, 0 if it is found.  */
  77.  
  78. scan_file (filename)
  79.      char *filename;
  80. {
  81.   int len = strlen (filename);
  82.   if (!strcmp (filename + len - 4, ".elc"))
  83.     return scan_lisp_file (filename);
  84.   else if (!strcmp (filename + len - 3, ".el"))
  85.     return scan_lisp_file (filename);
  86.   else
  87.     return scan_c_file (filename);
  88. }
  89.  
  90. char buf[128];
  91.  
  92. /* Skip a C string from INFILE,
  93.  and return the character that follows the closing ".
  94.  If printflag is positive, output string contents to outfile.
  95.  If it is negative, store contents in buf.
  96.  Convert escape sequences \n and \t to newline and tab;
  97.  discard \ followed by newline.  */
  98.  
  99. read_c_string (infile, printflag)
  100.      FILE *infile;
  101.      int printflag;
  102. {
  103.   register int c;
  104.   char *p = buf;
  105.  
  106.   c = getc (infile);
  107.   while (c != EOF)
  108.     {
  109.       while (c != '"' && c != EOF)
  110.     {
  111.       if (c == '\\')
  112.         {
  113.           c = getc (infile);
  114.           if (c == '\n')
  115.         {
  116.           c = getc (infile);
  117.           continue;
  118.         }
  119.           if (c == 'n')
  120.         c = '\n';
  121.           if (c == 't')
  122.         c = '\t';
  123.         }
  124.       if (printflag > 0)
  125.         putc (c, outfile);
  126.       else if (printflag < 0)
  127.         *p++ = c;
  128.       c = getc (infile);
  129.     }
  130.       c = getc (infile);
  131.       if (c != '"')
  132.     break;
  133.       if (printflag > 0)
  134.     putc (c, outfile);
  135.       else if (printflag < 0)
  136.     *p++ = c;
  137.       c = getc (infile);
  138.     }
  139.  
  140.   if (printflag < 0)
  141.     *p = 0;
  142.  
  143.   return c;
  144. }
  145.  
  146. /* Write to file OUT the argument names of the function whose text is in BUF.
  147.    MINARGS and MAXARGS are the minimum and maximum number of arguments.  */
  148.  
  149. write_c_args (out, buf, minargs, maxargs)
  150.      FILE *out;
  151.      char *buf;
  152.      int minargs, maxargs;
  153. {
  154.   register char *p;
  155.   int in_ident = 0;
  156.   int just_spaced = 0;
  157.  
  158.   fprintf (out, "arguments: ");
  159.  
  160.   for (p = buf; *p; p++)
  161.     {
  162.       char c = *p;
  163.       int ident_start = 0;
  164.  
  165.       /* Notice when we start printing a new identifier.  */
  166.       if ((('A' <= c && c <= 'Z')
  167.        || ('a' <= c && c <= 'z')
  168.        || ('0' <= c && c <= '9')
  169.        || c == '_')
  170.       != in_ident)
  171.     {
  172.       if (!in_ident)
  173.         {
  174.           in_ident = 1;
  175.           ident_start = 1;
  176.  
  177.           if (minargs == 0 && maxargs > 0)
  178.         fprintf (out, "&optional ");
  179.           just_spaced = 1;
  180.  
  181.           minargs--;
  182.           maxargs--;
  183.         }
  184.       else
  185.         in_ident = 0;
  186.     }
  187.  
  188.       /* Print the C argument list as it would appear in lisp:
  189.      print underscores as hyphens, and print commas as spaces.
  190.      Collapse adjacent spaces into one.  */
  191.       if (c == '_') c = '-';
  192.       if (c == ',') c = ' ';
  193.  
  194.       /* In C code, `default' is a reserved word, so we spell it
  195.      `defalt'; unmangle that here.  */
  196.       if (ident_start
  197.       && strncmp (p, "defalt", 6) == 0
  198.       && ! (('A' <= p[6] && p[6] <= 'Z')
  199.         || ('a' <= p[6] && p[6] <= 'z')
  200.         || ('0' <= p[6] && p[6] <= '9')
  201.         || p[6] == '_'))
  202.     {
  203.       fprintf (out, "default");
  204.       p += 5;
  205.       in_ident = 0;
  206.       just_spaced = 0;
  207.     }
  208.       else if (c != ' ' || ! just_spaced)
  209.     putc (c, out);
  210.  
  211.       just_spaced = (c == ' ');
  212.     }
  213. }
  214.  
  215. /* Read through a c file.  If a .o file is named,
  216.    the corresponding .c file is read instead.
  217.    Looks for DEFUN constructs such as are defined in ../src/lisp.h.
  218.    Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED.  */
  219.  
  220. scan_c_file (filename)
  221.      char *filename;
  222. {
  223.   FILE *infile;
  224.   register int c;
  225.   register int commas;
  226.   register int defunflag;
  227.   register int defvarperbufferflag;
  228.   register int defvarflag;
  229.   int minargs, maxargs;
  230.  
  231.   if (filename[strlen (filename) - 1] == 'o')
  232.     filename[strlen (filename) - 1] = 'c';
  233.  
  234.   infile = fopen (filename, "r");
  235.  
  236.   /* No error if non-ex input file */
  237.   if (infile == NULL)
  238.     {
  239.       perror (filename);
  240.       return 0;
  241.     }
  242.  
  243.   c = '\n';
  244.   while (!feof (infile))
  245.     {
  246.       if (c != '\n')
  247.     {
  248.       c = getc (infile);
  249.       continue;
  250.     }
  251.       c = getc (infile);
  252.       if (c == ' ')
  253.     {
  254.       while (c == ' ')
  255.         c = getc (infile);
  256.       if (c != 'D')
  257.         continue;
  258.       c = getc (infile);
  259.       if (c != 'E')
  260.         continue;
  261.       c = getc (infile);
  262.       if (c != 'F')
  263.         continue;
  264.       c = getc (infile);
  265.       if (c != 'V')
  266.         continue;
  267.       c = getc (infile);
  268.       if (c != 'A')
  269.         continue;
  270.       c = getc (infile);
  271.       if (c != 'R')
  272.         continue;
  273.       c = getc (infile);
  274.       if (c != '_')
  275.         continue;
  276.  
  277.       defvarflag = 1;
  278.       defunflag = 0;
  279.  
  280.       c = getc (infile);
  281.       defvarperbufferflag = (c == 'P');
  282.  
  283.       c = getc (infile);
  284.     }
  285.       else if (c == 'D')
  286.     {
  287.       c = getc (infile);
  288.       if (c != 'E')
  289.         continue;
  290.       c = getc (infile);
  291.       if (c != 'F')
  292.         continue;
  293.       c = getc (infile);
  294.       defunflag = c == 'U';
  295.       defvarflag = 0;
  296.     }
  297.       else continue;
  298.  
  299.       while (c != '(')
  300.     {
  301.       if (c < 0)
  302.         goto eof;
  303.       c = getc (infile);
  304.     }
  305.  
  306.       c = getc (infile);
  307.       if (c != '"')
  308.     continue;
  309.       c = read_c_string (infile, -1);
  310.  
  311.       if (defunflag)
  312.     commas = 5;
  313.       else if (defvarperbufferflag)
  314.     commas = 2;
  315.       else if (defvarflag)
  316.     commas = 1;
  317.       else  /* For DEFSIMPLE and DEFPRED */
  318.     commas = 2;
  319.  
  320.       while (commas)
  321.     {
  322.       if (c == ',')
  323.         {
  324.           commas--;
  325.           if (defunflag && (commas == 1 || commas == 2))
  326.         {
  327.           do
  328.             c = getc (infile);
  329.           while (c == ' ' || c == '\n' || c == '\t');
  330.           if (c < 0)
  331.             goto eof;
  332.           ungetc (c, infile);
  333.           if (commas == 2) /* pick up minargs */
  334.             fscanf (infile, "%d", &minargs);
  335.           else /* pick up maxargs */
  336.             if (c == 'M' || c == 'U') /* MANY || UNEVALLED */
  337.               maxargs = -1;
  338.             else
  339.               fscanf (infile, "%d", &maxargs);
  340.         }
  341.         }
  342.       if (c < 0)
  343.         goto eof;
  344.       c = getc (infile);
  345.     }
  346.       while (c == ' ' || c == '\n' || c == '\t')
  347.     c = getc (infile);
  348.       if (c == '"')
  349.     c = read_c_string (infile, 0);
  350.       while (c != ',')
  351.     c = getc (infile);
  352.       c = getc (infile);
  353.       while (c == ' ' || c == '\n' || c == '\t')
  354.     c = getc (infile);
  355.  
  356.       if (c == '"')
  357.     {
  358.       putc (037, outfile);
  359.       putc (defvarflag ? 'V' : 'F', outfile);
  360.       fprintf (outfile, "%s\n", buf);
  361.       c = read_c_string (infile, 1);
  362.  
  363.       /* If this is a defun, find the arguments and print them.  If
  364.          this function takes MANY or UNEVALLED args, then the C source
  365.          won't give the names of the arguments, so we shouldn't bother
  366.          trying to find them.  */
  367.       if (defunflag && maxargs != -1)
  368.         {
  369.           char argbuf[1024], *p = argbuf;
  370.           while (c != ')')
  371.         {
  372.           if (c < 0)
  373.             goto eof;
  374.           c = getc (infile);
  375.         }
  376.           /* Skip into arguments.  */
  377.           while (c != '(')
  378.         {
  379.           if (c < 0)
  380.             goto eof;
  381.           c = getc (infile);
  382.         }
  383.           /* Copy arguments into ARGBUF.  */
  384.           *p++ = c;
  385.           do
  386.         *p++ = c = getc (infile);
  387.           while (c != ')');
  388.           *p = '\0';
  389.           /* Output them.  */
  390.           fprintf (outfile, "\n\n");
  391.           write_c_args (outfile, argbuf, minargs, maxargs);
  392.         }
  393.     }
  394.     }
  395.  eof:
  396.   fclose (infile);
  397.   return 0;
  398. }
  399.  
  400. /* Read a file of Lisp code, compiled or interpreted.
  401.  Looks for
  402.   (defun NAME ARGS DOCSTRING ...)
  403.   (defmacro NAME ARGS DOCSTRING ...)
  404.   (autoload (quote NAME) FILE DOCSTRING ...)
  405.   (defvar NAME VALUE DOCSTRING)
  406.   (defconst NAME VALUE DOCSTRING)
  407.   (fset (quote NAME) (make-byte-code ... DOCSTRING ...))
  408.   (fset (quote NAME) #[... DOCSTRING ...])
  409.   (defalias (quote NAME) #[... DOCSTRING ...])
  410.  starting in column zero.
  411.  (quote NAME) may appear as 'NAME as well.
  412.  For defun, defmacro, and autoload, we know how to skip over the arglist.
  413.  For defvar, defconst, and fset we skip to the docstring with a kludgy 
  414.  formatting convention: all docstrings must appear on the same line as the
  415.  initial open-paren (the one in column zero) and must contain a backslash 
  416.  and a double-quote immediately after the initial double-quote.  No newlines
  417.  must appear between the beginning of the form and the first double-quote.
  418.  The only source file that must follow this convention is loaddefs.el; aside
  419.  from that, it is always the .elc file that we look at, and they are no
  420.  problem because byte-compiler output follows this convention.
  421.  The NAME and DOCSTRING are output.
  422.  NAME is preceded by `F' for a function or `V' for a variable.
  423.  An entry is output only if DOCSTRING has \ newline just after the opening "
  424.  */
  425.  
  426. void
  427. skip_white (infile)
  428.      FILE *infile;
  429. {
  430.   char c = ' ';
  431.   while (c == ' ' || c == '\t' || c == '\n')
  432.     c = getc (infile);
  433.   ungetc (c, infile);
  434. }
  435.  
  436. void
  437. read_lisp_symbol (infile, buffer)
  438.      FILE *infile;
  439.      char *buffer;
  440. {
  441.   char c;
  442.   char *fillp = buffer;
  443.  
  444.   skip_white (infile);
  445.   while (1)
  446.     {
  447.       c = getc (infile);
  448.       if (c == '\\')
  449.     *(++fillp) = getc (infile);
  450.       else if (c == ' ' || c == '\t' || c == '\n' || c == '(' || c == ')')
  451.     {
  452.       ungetc (c, infile);
  453.       *fillp = 0;
  454.       break;
  455.     }
  456.       else
  457.     *fillp++ = c;
  458.     }
  459.  
  460.   if (! buffer[0])
  461.     fprintf (stderr, "## expected a symbol, got '%c'\n", c);
  462.   
  463.   skip_white (infile);
  464. }
  465.  
  466.  
  467. scan_lisp_file (filename)
  468.      char *filename;
  469. {
  470.   FILE *infile;
  471.   register int c;
  472.  
  473.   infile = fopen (filename, "r");
  474.   if (infile == NULL)
  475.     {
  476.       perror (filename);
  477.       return 0;                /* No error */
  478.     }
  479.  
  480.   c = '\n';
  481.   while (!feof (infile))
  482.     {
  483.       char buffer [BUFSIZ];
  484.       char *fillp = buffer;
  485.       char type;
  486.  
  487.       if (c != '\n')
  488.     {
  489.       c = getc (infile);
  490.       continue;
  491.     }
  492.       c = getc (infile);
  493.       if (c != '(')
  494.     continue;
  495.  
  496.       read_lisp_symbol (infile, buffer);
  497.  
  498.       if (! strcmp (buffer, "defun") ||
  499.       ! strcmp (buffer, "defmacro"))
  500.     {
  501.       type = 'F';
  502.       read_lisp_symbol (infile, buffer);
  503.  
  504.       /* Skip the arguments: either "nil" or a list in parens */
  505.  
  506.       c = getc (infile);
  507.       if (c == 'n') /* nil */
  508.         {
  509.           if ((c = getc (infile)) != 'i' ||
  510.           (c = getc (infile)) != 'l')
  511.         {
  512.           fprintf (stderr, "## unparsable arglist in %s (%s)\n",
  513.                buffer, filename);
  514.           continue;
  515.         }
  516.         }
  517.       else if (c != '(')
  518.         {
  519.           fprintf (stderr, "## unparsable arglist in %s (%s)\n",
  520.                buffer, filename);
  521.           continue;
  522.         }
  523.       else
  524.         while (c != ')')
  525.           c = getc (infile);
  526.       skip_white (infile);
  527.  
  528.       /* If the next three characters aren't `dquote bslash newline'
  529.          then we're not reading a docstring.
  530.        */
  531.       if ((c = getc (infile)) != '"' ||
  532.           (c = getc (infile)) != '\\' ||
  533.           (c = getc (infile)) != '\n')
  534.         {
  535. #ifdef DEBUG
  536.           fprintf (stderr, "## non-docstring in %s (%s)\n",
  537.                buffer, filename);
  538. #endif
  539.           continue;
  540.         }
  541.     }
  542.  
  543.       else if (! strcmp (buffer, "defvar") ||
  544.            ! strcmp (buffer, "defconst"))
  545.     {
  546.       char c1 = 0, c2 = 0;
  547.       type = 'V';
  548.       read_lisp_symbol (infile, buffer);
  549.  
  550.       /* Skip until the first newline; remember the two previous chars. */
  551.       while (c != '\n' && c >= 0)
  552.         {
  553.           c2 = c1;
  554.           c1 = c;
  555.           c = getc (infile);
  556.         }
  557.       
  558.       /* If two previous characters were " and \,
  559.          this is a doc string.  Otherwise, there is none.  */
  560.       if (c2 != '"' || c1 != '\\')
  561.         {
  562. #ifdef DEBUG
  563.           fprintf (stderr, "## non-docstring in %s (%s)\n",
  564.                buffer, filename);
  565. #endif
  566.           continue;
  567.         }
  568.     }
  569.  
  570.       else if (! strcmp (buffer, "fset") || ! strcmp (buffer, "defalias"))
  571.     {
  572.       char c1 = 0, c2 = 0;
  573.       type = 'F';
  574.  
  575.       c = getc (infile);
  576.       if (c == '\'')
  577.         read_lisp_symbol (infile, buffer);
  578.       else
  579.         {
  580.           if (c != '(')
  581.         {
  582.           fprintf (stderr, "## unparsable name in fset in %s\n",
  583.                filename);
  584.           continue;
  585.         }
  586.           read_lisp_symbol (infile, buffer);
  587.           if (strcmp (buffer, "quote"))
  588.         {
  589.           fprintf (stderr, "## unparsable name in fset in %s\n",
  590.                filename);
  591.           continue;
  592.         }
  593.           read_lisp_symbol (infile, buffer);
  594.           c = getc (infile);
  595.           if (c != ')')
  596.         {
  597.           fprintf (stderr,
  598.                "## unparsable quoted name in fset in %s\n",
  599.                filename);
  600.           continue;
  601.         }
  602.         }
  603.  
  604.       /* Skip until the first newline; remember the two previous chars. */
  605.       while (c != '\n' && c >= 0)
  606.         {
  607.           c2 = c1;
  608.           c1 = c;
  609.           c = getc (infile);
  610.         }
  611.       
  612.       /* If two previous characters were " and \,
  613.          this is a doc string.  Otherwise, there is none.  */
  614.       if (c2 != '"' || c1 != '\\')
  615.         {
  616. #ifdef DEBUG
  617.           fprintf (stderr, "## non-docstring in %s (%s)\n",
  618.                buffer, filename);
  619. #endif
  620.           continue;
  621.         }
  622.     }
  623.  
  624.       else if (! strcmp (buffer, "autoload"))
  625.     {
  626.       type = 'F';
  627.       c = getc (infile);
  628.       if (c == '\'')
  629.         read_lisp_symbol (infile, buffer);
  630.       else
  631.         {
  632.           if (c != '(')
  633.         {
  634.           fprintf (stderr, "## unparsable name in autoload in %s\n",
  635.                filename);
  636.           continue;
  637.         }
  638.           read_lisp_symbol (infile, buffer);
  639.           if (strcmp (buffer, "quote"))
  640.         {
  641.           fprintf (stderr, "## unparsable name in autoload in %s\n",
  642.                filename);
  643.           continue;
  644.         }
  645.           read_lisp_symbol (infile, buffer);
  646.           c = getc (infile);
  647.           if (c != ')')
  648.         {
  649.           fprintf (stderr,
  650.                "## unparsable quoted name in autoload in %s\n",
  651.                filename);
  652.           continue;
  653.         }
  654.         }
  655.       skip_white (infile);
  656.       if ((c = getc (infile)) != '\"')
  657.         {
  658.           fprintf (stderr, "## autoload of %s unparsable (%s)\n",
  659.                buffer, filename);
  660.           continue;
  661.         }
  662.       read_c_string (infile, 0);
  663.       skip_white (infile);
  664.  
  665.       /* If the next three characters aren't `dquote bslash newline'
  666.          then we're not reading a docstring.
  667.        */
  668.       if ((c = getc (infile)) != '"' ||
  669.           (c = getc (infile)) != '\\' ||
  670.           (c = getc (infile)) != '\n')
  671.         {
  672. #ifdef DEBUG
  673.           fprintf (stderr, "## non-docstring in %s (%s)\n",
  674.                buffer, filename);
  675. #endif
  676.           continue;
  677.         }
  678.     }
  679.  
  680. #ifdef DEBUG
  681.       else if (! strcmp (buffer, "if") ||
  682.            ! strcmp (buffer, "byte-code"))
  683.     ;
  684. #endif
  685.  
  686.       else
  687.     {
  688. #ifdef DEBUG
  689.       fprintf (stderr, "## unrecognised top-level form, %s (%s)\n",
  690.            buffer, filename);
  691. #endif
  692.       continue;
  693.     }
  694.  
  695.       /* At this point, there is a docstring that we should gobble.
  696.      The opening quote (and leading backslash-newline) have already
  697.      been read.
  698.        */
  699.       putc ('\n', outfile);
  700.       putc (037, outfile);
  701.       putc (type, outfile);
  702.       fprintf (outfile, "%s\n", buffer);
  703.       read_c_string (infile, 1);
  704.     }
  705.   fclose (infile);
  706.   return 0;
  707. }
  708.